//
//  LKBillDetailViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/6/30.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKBillDetailViewController.h"
#import "LKCashTradeViewController.h"
#import "LKRefundMoneyViewController.h"
#define  RowHeight   32
@interface LKBillDetailViewController ()

@property(nonatomic,strong)UIView *headerView;
@property(nonatomic,strong)UIImageView *payImageV;
@property(nonatomic,strong)UILabel *introLabel;
@property(nonatomic,strong)UILabel *moneyLabel;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)UIView *detailView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation LKBillDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"交易详情";
    [self getDataInformation];
    // Do any additional setup after loading the view.
}



-(void)createHeaderShowView{
    self.headerView = [[UIView alloc] init];
    self.headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(196);
    }];
    self.payImageV = [[UIImageView alloc] init];
    NSString *payStyle = _model.paystyle_id;
    if ([payStyle isEqualToString:@"1"]) {
        self.payImageV.image = [UIImage imageNamed:@"ic_payment_we_chat"];
    }else if ([payStyle isEqualToString:@"2"]){
        self.payImageV.image = [UIImage imageNamed:@"ic_payment_ali_pay"];
    }else if ([payStyle isEqualToString:@"3"]){
        self.payImageV.image = [UIImage imageNamed:@""];
    }else if ([payStyle isEqualToString:@"4"]){
        self.payImageV.image = [UIImage imageNamed:@""];
    }else if ([payStyle isEqualToString:@"5"]){
        self.payImageV.image = [UIImage imageNamed:@"ic_payment_cash"];
    }else if ([payStyle isEqualToString:@"6"]){
        self.payImageV.image = [UIImage imageNamed:@""];
    }else if ([payStyle isEqualToString:@"7"]){
        self.payImageV.image = [UIImage imageNamed:@""];
    }else if ([payStyle isEqualToString:@"8"]){
        self.payImageV.image = [UIImage imageNamed:@""];
    }else if ([payStyle isEqualToString:@"10"]){
        self.payImageV.image = [UIImage imageNamed:@""];
    }
    else if ([payStyle isEqualToString:@"9"]){
        self.payImageV.image = [UIImage imageNamed:@"ic_payment_cloud_pay"];
    }
    [self.headerView addSubview:_payImageV];
    [_payImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_top).offset(20);
        make.centerX.equalTo(self.headerView);
        make.size.mas_equalTo(CGSizeMake(62, 62));
    }];
    self.introLabel = [LKBaseLabel labelWithFrame:CGRectZero text:[NSString stringWithFormat:@"%@交易金额",self.model.paystyle_name] textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentCenter];
    [self.headerView addSubview:self.introLabel];
    [self.introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_payImageV.mas_bottom).offset(10);
        make.centerX.equalTo(self.headerView);
    }];
    NSString *priceStr = @"";
    if (![_model.status isEqualToString:@"5"]) {
        priceStr = [NSString stringWithFormat:@"￥%@",self.model.price];
    }else{
       priceStr = [NSString stringWithFormat:@"-￥%@",self.model.price];
    }
    self.moneyLabel = [LKBaseLabel labelWithFrame:CGRectZero text:priceStr textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"DINAlternate-Bold" size:36] textAlignment:NSTextAlignmentCenter];
    [self.headerView addSubview:self.moneyLabel];
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_introLabel.mas_bottom).offset(10);
        make.centerX.equalTo(self.headerView);
    }];
    self.lineView = [[UIView alloc] init];
//    self.lineView.backgroundColor = COLOR_HEX(0xD4D4D4);
        self.lineView.backgroundColor = viewDefualtColor;
    [self.view addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(33);
        make.right.equalTo(self.view.mas_right).offset(-33);
        make.top.equalTo(self.moneyLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(1);
    }];
}
-(void)createFormView{
    NSString *payTime = [NSString stringWithFormat:@"%@",[NSString timestampSwitchTime:[self.model.paytime integerValue] andFormatter:@"YYYY-MM-dd HH:mm:ss"]];
    WS(weakSelf)
    NSString *priceStr = @"";
    if (![_model.status isEqualToString:@"5"]) {
        priceStr = [NSString stringWithFormat:@"￥%@",self.model.price];
    }else{
       priceStr = [NSString stringWithFormat:@"-￥%@",self.model.price];
    }
    [self createPayInforDetailDataViewWithTilesArray:@[@"订单金额",@"付款方式",@"付款渠道",@"交易手续费",@"操作员工",@"流水号",@"交易时间",@"备注"] andValuesArray:@[priceStr,self.model.paystyle_name,self.model.mode_name,self.model.discount_money,self.model.checker_name,self.model.remark,payTime,self.model.jmt_remark]];
    LKBaseButton *bottomBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"退款" titleColor:[UIColor whiteColor] layercorne:25 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:yellowDefaultColor AndActionBlock:^(UIButton * _Nonnull btn) {
        LKRefundMoneyViewController *vc = [[LKRefundMoneyViewController alloc] init];
        vc.price = weakSelf.model.price;
        vc.remark = weakSelf.model.remark;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [self.view addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-10);
        make.height.mas_equalTo(50);
        make.left.equalTo(self.view).offset(10);
        make.centerX.equalTo(self.view);
    }];
    if ([self.model.status isEqualToString:@"1"]) {
        bottomBtn.hidden = NO;
    }else{
        bottomBtn.hidden = YES;
    }
    
}

-(void)createPayInforDetailDataViewWithTilesArray:(NSArray *)titlesArray andValuesArray:(NSArray *)valuesArray{
    self.detailView = [[UIView alloc] init];
    self.detailView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.detailView];
    [self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.top.equalTo(self.lineView.mas_bottom).offset(10);
        make.height.mas_equalTo(8*RowHeight);
    }];
    NSMutableArray *viewsArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < titlesArray.count; i++) {
        UIView *subView = [[UIView alloc] init];
        subView.backgroundColor = [UIColor whiteColor];
        [self.detailView addSubview:subView];
        LKBaseLabel *titleLabel = [LKBaseLabel labelWithFrame:CGRectZero text:titlesArray[i] textColor:COLOR_HEX(0x999999) font:[UIFont fontWithName:@"PingFangSC-Regular" size:16] textAlignment:NSTextAlignmentLeft];
        [subView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(subView.mas_left).offset(20);
            make.centerY.equalTo(subView);
        }];
        LKBaseLabel *valueLabel = [LKBaseLabel labelWithFrame:CGRectZero text:valuesArray[i] textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentRight];
        [subView addSubview:valueLabel];
        [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(subView.mas_right).offset(-20);
            make.centerY.equalTo(subView);
        }];
        [viewsArray addObject:subView];
    }
    [viewsArray mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:5 leadSpacing:0 tailSpacing:0];
    [viewsArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(WIDTH);
    }];
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
/// 获取详情
-(void)getDataInformation{
    [SVProgressHUD showWithStatus:@"加载中..."];
    NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"remark":_model.remark,@"status":_model.status}];
    NSString *sign = [NSString encoingWithDic:param];
    [param setObject:sign forKey:@"sign"];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MypayBillDetailUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            NSDictionary *dic = responseObject[@"data"];
            self.model = [LKBillListModel modelWithDictionary:dic];
            [self createHeaderShowView];
            [self createFormView];
        }else{
            
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
